package com.draven.mall.controller.merchant;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.Product;
import com.draven.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商家商品管理控制器
 */
@RestController
@RequestMapping("/api/merchant/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页查询商品列表
     */
    @GetMapping("/list")
    public ApiResponse getProductList(
            @RequestParam Long merchantId,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        
        Map<String, Object> result = productService.getProductPage(merchantId, categoryId, keyword, status, page, size);
        return ApiResponse.success(result);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("/{id}")
    public ApiResponse getProductDetail(@PathVariable Long id) {
        Product product = productService.getProductById(id);
        if (product != null) {
            return ApiResponse.success(product);
        } else {
            return ApiResponse.error("商品不存在");
        }
    }

    /**
     * 添加商品
     */
    @PostMapping("/add")
    public ApiResponse addProduct(@RequestBody Product product) {
        boolean success = productService.addProduct(product);
        if (success) {
            return ApiResponse.success("添加商品成功");
        } else {
            return ApiResponse.error("添加商品失败");
        }
    }

    /**
     * 更新商品
     */
    @PostMapping("/update")
    public ApiResponse updateProduct(@RequestBody Product product) {
        if (product.getId() == null) {
            return ApiResponse.error("商品ID不能为空");
        }
        
        boolean success = productService.updateProduct(product);
        if (success) {
            return ApiResponse.success("更新商品成功");
        } else {
            return ApiResponse.error("更新商品失败，可能商品不存在或无权操作");
        }
    }

    /**
     * 删除商品
     */
    @PostMapping("/delete")
    public ApiResponse deleteProduct(@RequestBody Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());
        Long merchantId = Long.parseLong(params.get("merchantId").toString());
        
        boolean success = productService.deleteProduct(id, merchantId);
        if (success) {
            return ApiResponse.success("删除商品成功");
        } else {
            return ApiResponse.error("删除商品失败，可能商品不存在或无权操作");
        }
    }

    /**
     * 更新商品状态（上架/下架）
     */
    @PostMapping("/status")
    public ApiResponse updateProductStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());
        Long merchantId = Long.parseLong(params.get("merchantId").toString());
        Integer status = Integer.parseInt(params.get("status").toString());
        
        boolean success = productService.updateProductStatus(id, merchantId, status);
        if (success) {
            return ApiResponse.success(status == 1 ? "商品上架成功" : "商品下架成功");
        } else {
            return ApiResponse.error("操作失败，可能商品不存在或无权操作");
        }
    }
} 